/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdh;
import java.sql.*;

/**
 *
 * @author tale
 */
public class Backend extends ServerInfo {

    public static final Backend getDefaultBackend(){
        return new Backend();
    }

    Connection con;

    public boolean test(){
        String query = "SELECT * FROM PLANS";
        boolean value = false;
        try{
            if (connect() == true){
                Statement st = con.createStatement();
                st.executeQuery(query);
                value = true;
            }
        }catch(SQLException e){
            System.out.println(e.toString());
        }finally{
            disconnect();
        }
        return value;
    }
    
    public boolean connect(){
        disconnect();
        try{
            Class.forName(getDriver());
        }catch(ClassNotFoundException e){
            System.out.println("Driver not found.");
            return false;
        }
        
        try{
            con = DriverManager.getConnection(getUrl(), username, password);
        }catch(SQLException e){
            System.out.println("Failed to connect to server.");
            System.out.println(e.toString());
            return false;
        }
        return true;
    }

    public boolean disconnect(){
        con = null;
        return true;
    }

    public Connection getConnection(){
        return con;
    }

}

